﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
namespace DAO
{
    public class DAO_Connect
    {
        // private static string s = ConfigurationManager.ConnectionStrings["ConnectString"].ConnectionString;
        //private static string s = @"Server=(local);UID=nguyentinh;PWD=kimlong;Database=QLBH";
        private static string s = @"Server=(local);Database=CafeApp;Trusted_Connection=True;";
        private static SqlConnection connect;
        public DAO_Connect(string sql)
        {
            s = sql;
        }
        public DAO_Connect()
        {
            try
            {
                s = @"Server=(local);Database=CafeApp;Trusted_Connection=True;";
                connect = new SqlConnection(s);
                connect.Open();
            }
            catch
            { }
        }
        public DAO_Connect(string server, string name, string pass)
        {
            try
            {
                s = @"Server=" + server + ";UID=" + name + ";PWD=" + pass + ";Database=QLBH";
                connect = new SqlConnection(s);
                connect.Open();
            }
            catch
            { }
        }
        public static SqlConnection Cmb()
        {
            SqlConnection cn = new SqlConnection(s);
            return cn;
        }
        public bool Open()
        {
            try
            {
                connect = new SqlConnection(s);
                connect.Open();
            }
            catch
            {
                return false;
            }
            return true;
        }
        public void Disconnect()
        {
           // if (connect != null)
            {
                connect.Close();
                //MessageBox.Show("Đã đóng kết nối", "Thông báo");
            }
        }
        public static DataTable ExcecuteQuery(string sql)
        {
            try
            {
                SqlConnection cn = new SqlConnection(s);
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = cn;
                cmd.CommandText = sql;
                SqlDataAdapter dt = new SqlDataAdapter(cmd);
                DataTable kq = new DataTable();
                dt.Fill(kq);
                cn.Close();
                return kq;
            }
            catch (Exception e)
            {
                return null;
            }
        }
        public static bool ExcuteNonQuery(string sql)
        {
            try
            {
                SqlConnection cn = new SqlConnection(s);
                cn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = cn;
                cmd.CommandText = sql;
                cmd.ExecuteNonQuery();
                cn.Close();
                return true;
            }
            catch 
            {
                return false;
            }
        }

        public static DataTable selectAllImage()
        { 
            SqlConnection cn = new SqlConnection(s);
            SqlDataAdapter dda = new SqlDataAdapter("getAllImage", cn);
            dda.SelectCommand.CommandType = CommandType.StoredProcedure;
            DataTable dt = new DataTable();
            if (cn.State == ConnectionState.Closed)
            {
                cn.Open();
            }
            dda.Fill(dt);
            cn.Close();
            return dt;
        }

        public static DataTable getImage(string manv)
        {
            SqlConnection cn = new SqlConnection(s);
            SqlDataAdapter dda = new SqlDataAdapter("getImg", cn);
            dda.SelectCommand.Parameters.AddWithValue("@manv", manv);
            dda.SelectCommand.CommandType = CommandType.StoredProcedure;
            DataTable dt = new DataTable();
            if (cn.State == ConnectionState.Closed)
            {
                cn.Open();
            }
            dda.Fill(dt);
            cn.Close();
            return dt;
        }
    }
}
